package landian.com.module_base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

/**
 * Created by Administrator on 2019-03-05.
 */

public abstract class BaseFragmentActivity<P extends BasePresenter> extends BaseActivity<P> {
    private FragmentManager mFragmentManager;
    private int fragmentViewId;
    private BaseFragment lastFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        fragmentViewId = setFragmentView();
        super.onCreate(savedInstanceState);
    }

    protected abstract int setFragmentView();

    public void changeFragment(BaseFragment fragment) {
        mFragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        if (lastFragment != null && !lastFragment.isHidden()) {
            transaction.hide(lastFragment);
        }
        if (!fragment.isAdded()) {
            transaction.add(fragmentViewId, fragment);
        } else {
            transaction.show(fragment);
        }
        lastFragment = fragment;
        transaction.commit();

//        if (!fragment.isAdded()) {
//            mFragmentManager.beginTransaction().hide(lastFragment).add(fragmentViewId, fragment, fragment.getTag()).commit();
//        } else {
//            mFragmentManager.beginTransaction().hide(lastFragment).show(fragment).commit();
//        }
//        lastFragment = fragment;
    }
}
